﻿#region Copyright (c) 2006-2011 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2011 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Widgetsphere.Generator.Common.GeneratorFramework;
using System.Xml;
using System.IO;
using Widgetsphere.Generator.Common.Util;
using Widgetsphere.Generator.Common;

namespace Widgetsphere.VsCodeTools
{
	internal partial class GenerateSettings : Form
	{
		#region Class Members

		private Dictionary<string, Type> _generatorMap = new Dictionary<string, Type>();
		private IGenerator _generator = null;

		#endregion

		#region Constructors

		public GenerateSettings()
		{
			InitializeComponent();
		}

		public GenerateSettings(IGenerator generator, List<Type> generatorTypeList, List<Type> wizardTypeList)
			: this()
		{
			try
			{
				_generator = generator;

				GlobalCacheFile globalCacheFile = new GlobalCacheFile();
				ModelCacheFile cacheFile = new ModelCacheFile(_generator);
				foreach (var v in generatorTypeList)
				{
					object[] arr = v.GetCustomAttributes(typeof(GeneratorProjectAttribute), true);
					if (arr.Length == 1)
					{
						GeneratorProjectAttribute attribute = (GeneratorProjectAttribute)arr[0];
						if (!globalCacheFile.ExcludeList.Contains(v.FullName))
						{
							_generatorMap.Add(attribute.Name, v);
							string typeName = "";
							if (attribute.CurrentType != null) typeName = attribute.CurrentType.ToString();
							TreeNode node = tvwProjects.Nodes.Add(typeName, attribute.Name);
							node.Tag = attribute;

							if (wizardTypeList != null)
							{
								node.Checked = wizardTypeList.Contains(v);
							}
							else
							{
								node.Checked = !cacheFile.ExcludeList.Contains(v.FullName);
							}							

						}
					}
				}

			}
			catch (Exception ex)
			{
				throw;
			}
		}

		#endregion

		#region Properties

		public List<Type> ExcludeList
		{
			get
			{
				List<Type> retval = new List<Type>();

				foreach (string key in _generatorMap.Keys)
				{
					retval.Add(_generatorMap[key]);
				}

				foreach (TreeNode n in tvwProjects.Nodes)
				{
					if (n.Checked)
						retval.Remove(_generatorMap[n.Text]);
				}
				return retval;
			}
		}

		#endregion

		#region Event Handlers

		private void cmdOK_Click(object sender, EventArgs e)
		{
			ModelCacheFile cacheFile = new ModelCacheFile(_generator);
			cacheFile.ExcludeList.Clear();
			foreach (Type t in this.ExcludeList)
			{
				cacheFile.ExcludeList.Add(t.FullName);
			}
			cacheFile.Save();

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void tvwProjects_AfterSelect(object sender, TreeViewEventArgs e)
		{
			List<TreeNode> nodeList = new List<TreeNode>();
			foreach (TreeNode itemNode in tvwProjects.Nodes)
				nodeList.Add(itemNode);

			lstDependency.Items.Clear();
			if (tvwProjects.SelectedNode == null) return;

			//Now setup the dependencies
			GeneratorProjectAttribute attribute = tvwProjects.SelectedNode.Tag as GeneratorProjectAttribute;
			txtDescription.Text = attribute.Description;
			if (attribute.DependencyList != null)
			{
				foreach (string dependencyName in attribute.DependencyList)
				{
					TreeNode dependencyNode = nodeList.Where(x => x.Name == dependencyName).FirstOrDefault();
					if (dependencyNode != null)
					{
						lstDependency.Items.Add(dependencyNode.Text);
					}
				}
			}

		}

		#endregion

	}
}